package com.sky.service.common.Impl;

import com.sky.exception.UploadToAliossException;
import com.sky.result.Result;
import com.sky.service.common.CommonService;
import com.sky.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Override
    public Result<String> upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String suffix = file.getOriginalFilename().substring(originalFilename.lastIndexOf(".") );
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
        String upload = null;
        try {
            upload = aliOssUtil.upload(file.getBytes(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UploadToAliossException("上传失败");
        }
        return Result.success(upload);
    }
}
